package com.atguigu.url.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/17 15:02
 * @description:
 */
@Component
public class StatisticsUtil {

    @Autowired
    MD5DigestUtil md5DigestUtil;
    @Autowired
    StringRedisTemplate redisTemplate;


    /**
     * 统一一批次url下载存储总耗时
     *
     * @param urlSet
     * @return
     */
    public long statistics1BatchUrlDownloadAllTime(Set<String> urlSet) {
        System.out.println(" 执行 ==> statistics1BatchUrlDownloadAllTime(Set<String> urlSet) ");

        long allTime = 0L;

        for (String url : urlSet) {
            String urlMd5 = md5DigestUtil.getMD5(url);
            String key = "time" + urlMd5;

            String val = redisTemplate.opsForValue().get(key);
            long time1 = Long.parseLong(val);

            allTime += time1;
        }

        return allTime;
    }
}

